(
	function (factory) {
		if (typeof define === 'function' && define.amd) {
			define(['jquery',"underscore","Waves"], function($,_,wavesjs) {
				factory($,_,wavesjs);
			});
		} else if (typeof module === 'object' && module.exports) {
			var wavesjs = require('Waves');
			var _ = require('underscore');
			module.exports = function( root, jQuery ) {
				if ( jQuery === undefined ) {
					if ( typeof window !== 'undefined' ) {
						jQuery = require('jquery');
					}
					else {
						jQuery = require('jquery')(root);
					}
				}
				factory(jQuery,_,wavesjs);
			};
		} else {
			factory(jQuery,_,Waves);
		}
	}(
	function ($,_,Waves){
		"use strict";
		var extAssist = function() {
			this.elementBody=$("body");
		};
		extAssist.prototype= {
			initWaves: function () {
				if (typeof Waves !== "undefined") Waves.init();
			},
			initTilte:function(title){
				$("title").text(title);
			},
			initUpScroll: function () {
				var self=this;
				var upScroll = $("#btn-scroll-up");
				$(window).scroll(function () {
					if ($(window).scrollTop() > 100) {
						upScroll.addClass("display")
					} else {
						if (upScroll.hasClass("display")) {
							upScroll.removeClass("display")
						}
					}
				});
				upScroll.off("click").on("click", function () {
					self.bodyScrollTop(0,300);
				});
			},
			bodyScrollTop:function(top,time){
				top=top||0;
				time=time||0;
				$('html,body').animate({scrollTop: top+'px'}, time);
			},
			computeMenuMove:function(menuId){
				var self=this;
				var li=$("#menu_"+menuId).parent();
				if(li.length>0){
					if(!li.hasClass("active")){self._cancelNavMenu()}
					li.addClass("active");
					setTimeout(function () {
						var ulParent,liParent;
						var ul=li.parent();
						if(ul.hasClass("submenu")){
							liParent=ul.parent("li.has-submenu");
							ulParent=liParent.parent();
							liParent.addClass("active");
						}
						if(ul.hasClass("navigation-menu")){
							ulParent=ul;
							liParent=li;
						}
						var lenUl=ulParent.width();
						var lenLi=liParent.width();
						var ind=liParent.index();
						var l=0,r=0;
						if(ind===0){
							l=0;
							r=lenUl-lenLi;
						}else if(ind===ulParent.length-1){
							l=lenUl-lenLi;
							r=0;
						}else{
							$.each(liParent.prevAll("li"),function (i,item) {return l+=$(item).width();});
							$.each(liParent.nextAll("li"),function (i,item) {return r+=$(item).width();});
						}
						$("#navigation").find(".indicator").animate({left:l,right:r});
					},50)
				}else{
					$("#navigation").find(".indicator").css({left:"auto",right:"auto"});
					self._cancelNavMenu();
				}
			},
			_cancelNavMenu:function () {
				$("#navigation").find(".navigation-menu li").removeClass("active");
			},
			init: function () {
				var self = this;
				self.pageGlobalData={};
				$(function () {
					self.initWaves();
					self.winResize.init();
					self.winUnload.init();
					self.menu.init();
					self.bodyScrollTop(0);
					self._initAjaxGlobal();
					self._initDataTableGlobal();
				});
			},
			initLayoutIndex:function(){
				this.elementWrapper=$("#wrapper");
				this.initUpScroll();
				this.bodyScrollTop(0);
			},
			goUrlHref:function(url,isPromptly,isParam){
				isPromptly= $.ExtCommon.isCheckBoolean(isPromptly)?isPromptly:false;
                isParam= $.ExtCommon.isCheckBoolean(isParam)?isParam:true;
				var newUrl=url;
				if(url!=="#login" && isParam){
                    newUrl=this.urlParam.getNewUrl(url);
				}
				isPromptly?$.ExtCommon.goHref(newUrl):$.ExtCommon.goLocationHref(newUrl);
			},
			_initAjaxGlobal:function () {
				var self=this;
				self.pageGlobalData.logoutCode=["10001","20000","20001","20002","20003","20004","20203"];
				if($.ExtAjaxGlobal){}
				else{
					$.ExtAjaxGlobal={token:null, convertResultCallback:null}
				}
				$.ExtAjaxGlobal.convertResultCallback=function(res){
					var temp={reqtype:"ret",success:false,msg:"",data:null};
					temp.code=res.reqtype?res.code:res.ret;
					if(!$.ExtCommon.isCheckEmpty(temp.code)){temp.code=temp.code.toString()}
					if(temp.code==="200"){
						temp.success = true;
						temp.data = res.data;
					}else{
						if(self.pageGlobalData.logoutCode.indexOf(temp.code)>-1){
							window.location.href = "#login";
							return {success:false};
						}else {
							temp.success = false;
							temp.msg=$.ExtAjax.getCodeMsg(temp.code,res.err_msg);
						}
					}
					temp.err_msg=temp.msg;
					return temp
				}
			},
			_initDataTableGlobal:function () {},
			menu:{
				init:function(){
					this._operate=[];
				},
				computeMove:function(menuId){
					var self=this;
					var li=$("#menu_"+menuId).parent();
					if(li.length>0){
						if(!li.hasClass("active")){self._cancel()}
						li.addClass("active");
						setTimeout(function () {
							var ulParent,liParent;
							var ul=li.parent();
							if(ul.hasClass("submenu")){
								liParent=ul.parent("li.has-submenu");
								ulParent=liParent.parent();
								liParent.addClass("active");
							}
							if(ul.hasClass("navigation-menu")){
								ulParent=ul;
								liParent=li;
							}
							var lenUl=ulParent.width();
							var lenLi=liParent.width();
							var ind=liParent.index();
							var l=0,r=0;
							if(ind===0){
								l=0;
								r=lenUl-lenLi;
							}else if(ind===ulParent.length-1){
								l=lenUl-lenLi;
								r=0;
							}else{
								$.each(liParent.prevAll("li"),function (i,item) {return l+=$(item).width();});
								$.each(liParent.nextAll("li"),function (i,item) {return r+=$(item).width();});
							}
							$("#navigation").find(".indicator").animate({left:l,right:r});
						},50)
					}else{
						$("#navigation").find(".indicator").css({left:"auto",right:"auto"});
						self._cancel();
					}
				},
				_cancel:function () {
					$("#navigation").find(".navigation-menu li").removeClass("active");
				},
				checkAllowRun:function(){
					var check=true;
					if(this._operate.length>0 && this._operate[0].checkCallback){
						check=this._operate[0].checkCallback();
						check= $.ExtCommon.isCheckBoolean(check)?!check:true;
					}
					return check;
				},
				checkCallback:function(cb){
					if($.ExtCommon.isCheckUndefined(cb)){
						this._operate=[];
					}else{
						this._operate=[{checkCallback:cb}];
					}
				}
			},
            navBread:{
				homeUrl:"",
				title:function (title) {
					$("#panel_navBread").find(".page-operate-title").text(title);
                },
				nav:function (list) {
					var arr=[];
                    //if(!$.ExtCommon.isCheckEmpty(this.homeUrl)){arr.push('<li><a href="'+this.homeUrl+'">首页</a></li>')}
                    $.each(list,function (i,info) {
						var cls=($.ExtCommon.isCheckEmpty(info.cls)?"":info.cls+"")+(list.length-1===i?"page-operate-title":"");
                        if($.ExtCommon.isCheckEmpty(info.href)){
                            arr.push($.ExtCommon.getStringConnect('<li class="{1}">{0}</li>',[info.name,cls]));
						}else{
                            arr.push($.ExtCommon.getStringConnect('<li class="{2}"><a href="{1}" title="{0}">{0}</a></li>',[info.name,info.href,cls]));
						}
                    });
                    $("#panel_navBread").children(".breadcrumb").html(arr.join(""));
					this.title(list[list.length-1].name);
                },
                titleOnly:function (title) {
                    $("#panel_navBread").children("span.page-operate-title").text(title);
                },
                navLast:function (title) {
                    $("#panel_navBread").find(".breadcrumb .page-operate-title").text(title);
                }
			},
            modal:{
				computeTop:function (obj,h) {
					var self=this;
					setTimeout(function () {
						self.computeTopReal(obj,h)
					},200);
				},
				computeTopReal:function(obj,h){
					var element= typeof obj==="string"?$(obj):obj;
					var height=h||element.find(".modal-content").height();
					var totalHeight=$("body").height();
					var diff=totalHeight-height;
					var top=diff>10?Math.ceil(diff/2):5;
					element.children().animate({"margin-top":top});
				}
			},
			winResize:{
				init:function(){
					var self = this;
					self._operate=[];
					self._timeDelay={};
					$(window).resize(function(){
						if(self._operate.length>0){
							var info=self._operate[0];
							self._runTimeDelay(info.type,info.callback,info.time);
						}
					});
				},
				_runTimeDelay:function(type,callback,time){
					time=time||500;
					this._stopTimeRunDelay(type);
					this._timeDelay[type]=setTimeout(callback, time);
				},
				_stopTimeRunDelay:function(type){
					if (this._timeDelay && this._timeDelay[type]) {
						clearTimeout(this._timeDelay[type]);
						this._timeDelay[type] = null;
					}
				},
				callback:function(type,cb){
					if($.ExtCommon.isCheckUndefined(type)){
						this._operate=[];
					}else{
						this._operate=[{type:type,callback:cb,time:500}];
					}
				}
			},
			winUnload:{
				init:function(){
					var self = this;
					self._operate=[];
					$(window).unload(function(){
						if(self._operate.length>0 && self._operate[0].callback){
							self._operate[0].callback();
						}
					});
				},
				callback:function(cb){
					if($.ExtCommon.isCheckUndefined(cb)){
						this._operate=[];
					}else{
						this._operate=[{callback:cb}];
					}
				}
			},
			urlParam:{
                cleanParam:function () {
                    this.param={};
                },
				getParam:function () {
					return this.param||{};
                },
				setParam:function (json) {
                    json=json||{};
                    this.param=$.ExtCommon.initMergeJson(this.param, json);
                },
				isImitate:function () {
					return this.getParam().hasOwnProperty("imitateId");
                },
				setImitateId:function (imitateId) {
					if(!$.ExtCommon.isCheckEmpty(imitateId)){this.setParam({imitateId:imitateId})}
                },
				_getParamByUrl:function (url) {
					var temp={};
					var arrRegValue=url.match(/[&|?][^&]*=[^&]*/g);
					if(arrRegValue && arrRegValue.length>0){
						$.each(arrRegValue,function (i,info) {
							var regValue=info.match(/[&|?]([^&]*)=([^&]*)/);
							if(regValue&&regValue.length===3){
								temp[regValue[1]]=regValue[2]
							}
						});
					}
					return temp;
                },
				getNewUrl:function (url,isHash) {
                    isHash=$.ExtCommon.isCheckBoolean(isHash)?isHash:true;
					var newUrl=url;
					var tempUrl=url;
					if(isHash){
                        var first=url.indexOf("#");
                        tempUrl=first>-1?url.substr(first):"";
					}
					if(tempUrl.length>0){
                        var urlParam=this._getParamByUrl(tempUrl);
                        var last=tempUrl.indexOf("?");
                        var newParam= $.ExtCommon.initMergeJson(urlParam, this.getParam());
                        newUrl=(last>-1?tempUrl.substr(0,last):tempUrl)+($.ExtCommon.isCheckEmpty(newParam)?"":("?"+$.ExtCommon.getUrlParamByJson(newParam)));
					}
					return newUrl;
                },
                setUrl:function (element) {
					var self=this;
                    element=element||$("body a");
                    $.each(element,function(i,item){
                        var _this=$(item);
                        if(_this.length>0 && _this.is("a")){
                            var url=_this.attr("href");
                            var isLogo=url==="#"&&_this.hasClass("goHome");
                            if((!$.ExtCommon.isCheckEmpty(url,"#")&&url.indexOf("javascript:")===-1) || isLogo){
								var newUrl=self.getNewUrl(url);
                                _this.attr("href",newUrl);
							}
						}
					})
                }
			},
			pageListParam:{
				_init:function(pageParam){
					pageParam=$.ExtCommon.isCheckUndefined(pageParam)?{}:pageParam;
					$.cookie('pageListParam',JSON.stringify(pageParam));
				},
				_getParentData:function(){
					var pageParam=$.cookie("pageListParam");
					return $.ExtCommon.isCheckUndefined(pageParam)?{}:$.parseJSON(pageParam);
				},
				getData:function(name){
					var pageParam=this._getParentData();
					var data=pageParam[name];
					return $.ExtCommon.isCheckUndefined(data)?{}:$.ExtCommon.getJsonClone(data);
				},
				setData:function(name,data){
					var pageParam=this._getParentData();
					if($.ExtCommon.isCheckUndefined(data)){
						if(pageParam.hasOwnProperty(name)) {
							delete pageParam[name];
						}
					}else{
						pageParam[name]=data;
					}
					this._init(pageParam);
				},
				clean:function(){
					this._init();
				}
			},
			rolePower:{
				_getCurrent:function(cb){
					var self=this;
					if($.ExtCommon.isCheckUndefined(self.powerData)){
						self.timeCurrent=setTimeout(function(){self._getCurrent(cb)},50)
					}else{
						if(cb){cb(self.powerData)}
					}
				},
				setCurrent:function(result){
					if(result.res.success){
						this.powerData={page:result.req.page,original:result.res.data};
						this.powerData.data=this.analysisPower(this.powerData.original);
					}else{
						this.operateIllicit();
					}
				},
				cleanCurrent:function(){
					this.powerData=null;
					window.clearTimeout(this.timeCurrent);
				},
				operatePage:function(main,options,cb){
					var self=this;
					var isArrOptions=$.isArray(options);
					var operate=isArrOptions?options:options.operate;
					var special=isArrOptions?[]:options.special;
					if(main.indexOf("|")>-1){operate=operate.concat(main.split("|"))}
					else if(main.indexOf("&")>-1){operate=operate.concat(main.split("&"))}
					else {operate.push(main)}
					if(!$.ExtCommon.isCheckEmpty(operate)){
						this._getCurrent(function(json){
							var power=$.ExtCommon.getJsonClone(json);
							$.each(operate,function(i,info){
								var key=info;
								switch (info){
									case "create":
										key="add";
										break;
								}
								power[info]=self.checkPower(key,power.data);
							});
							var isMain=false;
							if(main.indexOf("|")>-1){
								var arrOr=main.split("|");
								var isOr=power[arrOr[0]];
								for(var o=1;o<arrOr.length;o++){isOr=isOr||power[arrOr[o]]}
								isMain=isOr;
							}else if(main.indexOf("&")>-1){
								var arrAnd=main.split("|");
								var isAnd=power[arrAnd[0]];
								for(var a=1;a<arrAnd.length;a++){isAnd=isAnd||power[arrAnd[a]]}
								isMain=arrAnd;
							}else {
								isMain=power[main];
							}
							if(isMain){
								$.each(operate,function(i,info){
									if(special.indexOf(info)===-1){
										self.operateElement(power,info);
									}
								});
								if(cb)(cb(power))
							}else{
								self.operateIllicit();
							}
						});
					}
				},
                operateElement:function (power,info) {
                    var element=$(".page-power-"+info);
                    if(element.length>0){
                        power[info]?this.showElement(element):this.removeElement(element);
                    }
                },
                showElement:function (id) {
                    var element=typeof id==="string"?$(".page-power-"+id):id;
					$.each(element,function (i,item) {
						if(item.style.display==="none"){
							$(item).show();
						}
                    });
                },
				removeElement:function(id){
					var element=typeof id==="string"?$(".page-power-"+id):id;
					element.remove();
				},
				getPageName:function(){
					var menu=$("#navigation").find("li:not(.has-submenu).active").children("a");
					var mapping=menu.data("mapping");
					return $.ExtCommon.isCheckEmpty(mapping)?"":mapping
				},
				analysisPower:function(list){
					var isTotal=false;
					var arrPower=[];
					if(list.length>0){
						$.each(list,function (i,info) {
							var temp=info.split(":");
							var power=temp[temp.length-1].toLowerCase();
							if(power==="total"){isTotal=true}
							arrPower.push(power);
						});
						arrPower= $.ExtCommon.getArrUnique(arrPower);
					}
					return {isTotal:isTotal,power:arrPower}
				},
				checkPower:function(type,json){
					var check=true;
					if(!$.ExtCommon.isCheckEmpty(type)){
						type=type.toLowerCase();
						if(json.isTotal){
							check=true;
						}else{
							check=json.power.indexOf(type)>-1;
						}
					}
					return check;
				},
				isTotal:function(json){return this.checkPower("total",json)},
				operateIllicit:function(href){
					var isGoLogin= $.ExtCommon.isCheckEmpty(href);
					var goHref=isGoLogin?"#login":href;
					var msg="用户没有该权限"+(isGoLogin?",请重新登录":"");
					$.ExtNotify.error(msg);
					$.ExtAssist.goUrlHref(goHref);
				}
			}
		};
		$.ExtAssist = new extAssist;
		$.ExtAssist.Constructor = extAssist;
		$.ExtAssist.init();
	})
);